package com.jwell.panrank.presenter

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jwell.driverapp.tools.BaseSubscriber
import com.jwell.driverapp.ui.base.BasePresenter
import com.jwell.panrank.contract.PreRegisterContract
import com.jwell.panrank.entity.ValMsgDto
import com.jwell.panrank.model.PreRegisterModel
import org.json.JSONObject
import org.reactivestreams.Subscription

/**
 * Created by 76577 on 2018/4/9.
 * 预登记
 */
class PreRegisterPresenter : PreRegisterContract.Presenter, BasePresenter<PreRegisterContract.View>() {


    private val model: PreRegisterModel by lazy {
        PreRegisterModel()
    }

    override fun checkCarNum(id: String, position: Int) {
        model.checkCarNum(id, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                var data = Gson().fromJson<ValMsgDto>(data.getString("result"), object : TypeToken<ValMsgDto>() {
                }.type)
                getView()?.showCheckCarNum(data, position)
            }

            override fun onFailed(msg: String) {
                getView()?.toast(msg)
            }
        })
    }

    override fun checkUnit(forwardingUnit: String?, receivingUnit: String?) {
        model.checkUnit(forwardingUnit, receivingUnit, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                if (data.getBoolean("result")) {
                    getView()?.havePermission(true)
                } else {
                    getView()?.havePermission(false)
                }
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.havePermission(false)
            }
        })
    }

    override fun checkDriverPhone(id: String, name: String?) {
        model.checkDriverPhone(id, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                if (data.getBoolean("result")) {
                    getView()?.showCheckDriverPhone(false, id, name)
                } else {
                    getView()?.showCheckDriverPhone(true, id, name)
                }
            }

            override fun onFailed(msg: String) {
                getView()?.toast(msg)
            }
        })
    }

    override fun createWaybill(data: PreRegisterModel.RequestData) {
        model.createWaybill(data, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                getView()?.createSucceed()
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.createFailed(msg)
            }
        })
    }
}